package org.springblade.task.job.report.sales;

import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springblade.common.utils.DateUtils;
import org.springblade.modules.salesAnalysis.entity.DailySalesIncomeReport;
import org.springblade.modules.salesAnalysis.service.IDailySalesIncomeReportService;
import org.springblade.task.base.entity.BaseVirtualMoneyFlowLog;
import org.springblade.task.base.param.BaseOrderBookParam;
import org.springblade.task.base.param.BaseParam;
import org.springblade.task.base.service.IBaseAudioChapterOrderService;
import org.springblade.task.base.service.IBaseEbookOrderService;
import org.springblade.task.base.service.IBaseNetChapterOrderService;
import org.springblade.task.base.service.IBaseVirtualMoneyFlowLogService;
import org.springblade.task.base.vo.BaseAudioChapterOrderVO;
import org.springblade.task.base.vo.BaseEbookOrderVO;
import org.springblade.task.base.vo.BaseNetChapterOrderVO;
import org.springblade.task.base.vo.BaseVirtualMoneyFlowLogVO;
import org.springblade.task.job.base.BaseTaskInfoJob;
import org.springblade.task.quartz.entity.QuartzTask;
import org.springblade.task.quartz.service.IQuartzTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @Title: 每日销量收入-销售分析
 * @version V1.0
 */
@Component
public class DailySalesIncomeReportJob {

	private static Logger logger = LoggerFactory.getLogger(BaseTaskInfoJob.class);

	@Autowired
	private IQuartzTaskService quartzTaskService;
	@Autowired
	private IBaseVirtualMoneyFlowLogService baseVirtualMoneyFlowLogService;
	@Autowired
	private IBaseEbookOrderService baseEbookOrderService;
	@Autowired
	private IBaseAudioChapterOrderService baseAudioChapterOrderService;
	@Autowired
	private IBaseNetChapterOrderService baseNetChapterOrderService;
	@Autowired
	private IDailySalesIncomeReportService dailySalesIncomeReportService;

	/**
	 * 1、简单任务示例（Bean模式）
	 */
	@XxlJob("dailySalesIncomeReportJob")
	public ReturnT<String> dailySalesIncomeReportJob(String param) throws Exception {
		System.out.println(DateUtils.currentDateTime() + " -- XXL-JOB, Hello World.");
		System.err.println("执行静态定时任务日期: " + DailySalesIncomeReportJob.class.getSimpleName());
		QuartzTask quartzTask = quartzTaskService.queryByTaskName(DailySalesIncomeReportJob.class.getSimpleName(), -7);
		// 今日
		String todayDate = DateUtils.crutDate();
		String taskDate = quartzTask.getTaskDate();
		while (taskDate.compareTo(todayDate) < 0) {
			// 当时间大于5点，跳出执行
//			if (DateUtils.getHour24() > 5) {
//				break;
//			}
			// 再加一个凌晨1点判断
			currMethod(taskDate);
			// 增加1天
			taskDate = DateUtils.dateAddDay(taskDate, 1);
		}
		quartzTask.setTaskDate(taskDate);
		quartzTaskService.saveQuartzTask(quartzTask);
		return ReturnT.SUCCESS;
	}

	/**
	 *
	 * @param taskDate
	 */
	public void currMethod(String taskDate) {

		BaseParam param = new BaseParam();
		param.setStartDate(DateUtils.getDateStartTime(taskDate));
		param.setEndDate(DateUtils.getDayEndTime(taskDate));
		// 充值
		BaseVirtualMoneyFlowLogVO virtualMoneyFlowLog =  baseVirtualMoneyFlowLogService.queryTodayRechargeAmount(param);

		BaseOrderBookParam bookParam = new BaseOrderBookParam();
		bookParam.setStartDate(DateUtils.getDateStartTime(taskDate));
		bookParam.setEndDate(DateUtils.getDayEndTime(taskDate));
		// 电子书订单
		BaseEbookOrderVO ebookOrderVO = baseEbookOrderService.queryTodayOrderAmount(bookParam);
		// 有声订单
		BaseAudioChapterOrderVO audioChapterOrderVO  = baseAudioChapterOrderService.queryTodayOrderAmount(bookParam);
		// 网文订单
		BaseNetChapterOrderVO netChapterOrderVO = baseNetChapterOrderService.queryTodayOrderAmount(bookParam);

		int payingUsers = 0; // 日付费用户数
		int repurchaseUsers = 0; // 日复购用户数
		int dailySales = 0; // 日销售额
		int customerPrice = 0; // 客单价
		int dailySalesVolume = 0; // 日销量
		int dailyRecharge = 0; // 日充值
		int dailyIncome = 0; // 日收入
		if(null != virtualMoneyFlowLog) {
			dailyRecharge = dailyRecharge + virtualMoneyFlowLog.getRechargeAmount();
		}
		if(null != ebookOrderVO) {
			payingUsers = payingUsers + ebookOrderVO.getPinNum();
			dailySales = dailySales + ebookOrderVO.getAmount();
			dailySalesVolume = dailySalesVolume + ebookOrderVO.getOrderNum();
		}
		if(null != audioChapterOrderVO) {
			payingUsers = payingUsers + audioChapterOrderVO.getPinNum();
			dailySales = dailySales + audioChapterOrderVO.getAmount();
			dailySalesVolume = dailySalesVolume + audioChapterOrderVO.getOrderNum();
		}
		if(null != netChapterOrderVO) {
			payingUsers = payingUsers + netChapterOrderVO.getPinNum();
			dailySales = dailySales + netChapterOrderVO.getAmount();
			dailySalesVolume = dailySalesVolume + netChapterOrderVO.getOrderNum();
		}
		dailyIncome = dailyRecharge + dailySales;
		customerPrice = new BigDecimal(dailySales).divide(new BigDecimal(payingUsers)).intValue();
		DailySalesIncomeReport report = new DailySalesIncomeReport();
		report.setPayingUsers(payingUsers);  // 日付费用户数
		report.setRepurchaseUsers(repurchaseUsers); // 日复购用户数
		report.setDailySales(dailySales);// 日销售额
		report.setCustomerPrice(customerPrice); // 客单价
		report.setDailySalesVolume(dailySalesVolume); // 日销量
		report.setDailyRecharge(dailyRecharge); // 日充值
		report.setDailyIncome(dailyIncome); // 日收入

		dailySalesIncomeReportService.save(report);
	}


	public void init() {
		logger.info("init");
	}

	public void destroy() {
		logger.info("destory");
	}

}
